package com.qf.controller;

import com.qf.entity.User;
import com.qf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用户管理的控制层：Servlet
 */
// http://localhost:8080/jgpy/user
// 注解类：路径索引
@WebServlet("/jgpy/user")
public class UserController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.doGet(req, resp);
        // HttpServletRequest:用户发送的请求
        // HttpServletResponse:服务器返回给请求的响应
        this.doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.doPost(req, resp);
        System.out.println("服务器-接收到你发送的请求");
        // 服务器在接收请求的时候 - 需要判断是你什么的请求
        //【1】 统一设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //【2】设置响应的类型
        resp.setContentType("text/html; charset=utf-8");
        //【3】设置Response的响应体：作用-会给数据给前端请求 - 获取字符输出流
        PrintWriter writer = resp.getWriter();
        // 【4】测试获取前端发送的请求数据
        // System.out.println("用户的操作："+req.getParameter("action"));
        // System.out.println("用户名："+req.getParameter("username"));
        // System.out.println("密码："+req.getParameter("password"));
        // // 一般情况 - 返回给前端的是一个json字符串格式的数据
        // String data = "{code:200,msg:'登录成功',data:''}";
        // writer.write(data);
        // writer.flush(); //清空缓存
        // writer.close(); //关闭响应输出流

        //【5】区分发送的请求
        String action = req.getParameter("action");
        switch (action){
            case "login":
                //登录功能
                String username = req.getParameter("username");
                String password = req.getParameter("password");
                // 调用服务层 - 用户处理数据
                String result = new UserService().login(username,password);
                writer.write(result);
                writer.flush();
                writer.close();
                break;
            case "register":
                //注册功能
                User user = new User();
                user.setFUsername(req.getParameter("username"));
                user.setFPassword(req.getParameter("password"));
                user.setFNickname(req.getParameter("nickname"));
                String createtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                user.setFCreatetime(createtime);
                user.setFHeadicon("1.jpg");
                user.setFMobilephone("18000123456");
                String result_register = new UserService().register(user);
                writer.write(result_register);
                writer.flush();
                writer.close();




                break;
        }




    }
}
